package com.hrbxlh.actionImpl;

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.hrbxlh.dao.ChapterDao;
import com.hrbxlh.dao.ClassDao;
import com.hrbxlh.dao.CourseDao;
import com.hrbxlh.dao.HomeworkTestDao;
import com.hrbxlh.dao.RelationDao;
import com.hrbxlh.util.ConfigFileUtil;
import com.hrbxlh.util.FileUpload;
import com.hrbxlh.util.SessionValues;
import com.hrbxlh.util.pageUtil;
import com.hrbxlh.vo.SkillDTO;
import com.hrbxlh.vo.SkillNotes;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：CourseActionImpl.java
* Description：课程actionImpl类,负责操作课程数据以及逻辑处理。
* History：
* Date           Author               Desc
* 2017.3.27		  xzx                 建立
* -------------------------------------------------------
*/

@Component("courseActionImpl")
@Transactional
public class CourseActionImpl {
	
	@Resource(name="courseDao")
	private CourseDao courseDao;
	
	@Resource(name="chapterDao")
	private ChapterDao chapterDao;
	
	@Resource(name="homeworkTestDao")
	private HomeworkTestDao homeworkTestDao;
	
	@Resource(name="classDao")
	private ClassDao classDao;
	
	@Resource(name="relationDao")
	private RelationDao relationDao;

	/**
	 * @description 添加课程的方法，需要课程名，副标题，封面颜色，讲师，简介
	 * @param Map<String,Object> paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public Map<String, Object> addCourse(Map<String, Object> paramMap) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> nameMap = courseDao.getCourseDetailByName(paramMap);
			if(nameMap == null){
				String introction = (String)paramMap.get("courseIntroduction");
				introction = introction.replaceAll("\n", "<br>");
				introction = introction.replaceAll("\r", "<br>");
				paramMap.put("courseIntroduction", introction);
				//添加课程
				courseDao.addCourse(paramMap); 
				//返回添加的课程信息
				Map<String, Object> detailMap = courseDao.getCourseDetail(paramMap);
				map.put("detailMsg", detailMap);
				//将添加的课程信息保存到中间表中
				detailMap.put("inMark", 1);
				detailMap.put("selectMark", 1);
				courseDao.addRelTeaCourse(detailMap);
				map.put("resultFlag", true);
			}else{
				map.put("resultFlag", false);
				map.put("failMsg", "课程名称已存在");
			}
		} catch (Exception e) {
			map.put("resultFlag", false);
			throw e;
		}
		return map;
	}
	
	/**
	 * @description 获取课程信息，参数是主键
	 * @param Map<String,Object> paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public Map<String, Object> getCourseDetail(Map<String, Object> paramMap,HttpServletRequest request) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request);
			//返回信息
			map = courseDao.getCourseDetail(paramMap);
			//用课程id与学号查询进度信息
			map.put("stuorteaId", sessionValues.get("stuorteaId"));
			Map<String, Object> processMap = courseDao.getMyCourseProcess(map);
			map.put("courseProcess", processMap.get("courseProcess"));
		} catch (Exception e) {
			throw e;
		}
		return map;
	}
	
	/**
	 * @description 修改课程
	 * @param Map<String,Object> paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public Map<String, Object> saveCourse(Map<String, Object> paramMap) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> listMap = courseDao.getCourseDetailByName(paramMap);
			if(listMap == null){
				String introction = (String)paramMap.get("courseIntroduction");
				introction = introction.replaceAll("\n", "<br>");
				introction = introction.replaceAll("\r", "<br>");
				paramMap.put("courseIntroduction", introction);
				courseDao.saveCourse(paramMap);
				map.put("result", true);
			}else{
				map.put("result", false);
				map.put("failMsg", "课程名称已存在");
			}
		} catch (Exception e) {
			throw e;
		}
		return map;
	}
	
	/**
	 * @description 获取登录人所属院系的所有专业信息
	 * @param Map<String,Object> paramMap  工号
	 * @return List<Map<String,Object>>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getMyProfessionList(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = courseDao.getMyProfessionList(paramMap);
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description 查询某教师教的课程列表
	 * @param Map<String,Object> paramMap
	 * @return List courseId课程id courseName课程名称
	 * @throws Exception
	 */
	public List<Map<String,Object>> getTeaTeachCourse(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = courseDao.getTeaTeachCourse(paramMap);
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description 删除课程的方法，传的是每个课程的id。 
	 * @param .List<String> paramList 课程id集合
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public boolean delCourse(List<String> paramList) throws Exception {

		boolean resultFlag = false;
		try {
			//0510删除教师与班级的关联关系
			courseDao.delTeaReClass(paramList);
			//0510删除学生与选课班级的关联信息
			courseDao.delStuReSelectClass(paramList);
			//0510删除选课班级的信息
			courseDao.delSelectClass(paramList);
			//删除课程
			courseDao.delCourse(paramList);
			//删除课程对应的章节信息
			courseDao.delChapter(paramList);
			//删除课程表信息
			courseDao.delChassTab(paramList);
			//删除课程与班级的对应关系
			courseDao.delCourseClass(paramList);
			//删除学生与课程的对应关系
			courseDao.delStuCourse(paramList);
			//删除考试与课程的对应关系
			courseDao.delCourseExam(paramList);
			//删除问题与课程的关系
			courseDao.delCourseProblem(paramList);
			//删除作业信息
			courseDao.delHomework(paramList);
			//删除题库信息
			courseDao.delCourseQuestion(paramList);
			//删除学生学习历史信息
			courseDao.delUserStudyHistory(paramList);
			//删除classRelation表中的数据  参数是courseId
			classDao.delClassRelationsByCIds(paramList);
			resultFlag = true;
		} catch (Exception e) {
			throw e;
		}
		return resultFlag;
	}
	
	/**
	 * @description 清空进度信息的方法
	 * @param 课程id
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public boolean emptyProcess(Map<String, Object> paramMap) throws Exception {

		boolean resultFlag = false;
		try {
			//清除进度信息
			courseDao.emptyProcess(paramMap);
			//清除学习信息（学号，课程号）
			courseDao.delMyStudyHistory(paramMap);
			resultFlag = true;
		} catch (Exception e) {
			throw e;
		}
		return resultFlag;
	}
	
	/**
	 * @description 添加进度信息的方法
	 * @param 课程id
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public boolean addProcess(Map<String, Object> paramMap) throws Exception {

		boolean resultFlag = false;
		try {
			//获取原来他自己的进度信息
			Map<String, Object> processDetail = courseDao.getMyCourseProcess(paramMap);
			long process = (Long)processDetail.get("courseProcess");
			process += 1;
			int chapterId = (Integer)paramMap.get("chapterId");
			paramMap.remove("chapterId");
			//如果比所有的章节数都长就相等
			List<Map<String, Object>> chapterListAll = chapterDao.getCourseChapter(paramMap);
			if(chapterListAll != null && chapterListAll.size() != 0){
				int chapterLength = chapterListAll.size();
				if(process > chapterLength){
					process = chapterLength;
				}
			}
			processDetail.put("courseProcess", process);
			//添加进度信息
			processDetail.put("chapterId", chapterId);
			processDetail.put("stuorteaId", paramMap.get("stuorteaId"));
			processDetail.put("courseId", paramMap.get("courseId"));
			courseDao.addProcess(processDetail);
			//添加学习章节信息
			paramMap.put("chapterId", chapterId);
			List<Map<String, Object>> chapterList = courseDao.getMyStudyChapterList(paramMap);
			if(chapterList == null || chapterList.size() == 0){
				//获取登录人与这个课的关联标识
				Map<String, Object> relationMap = homeworkTestDao.getRelationMark(paramMap);
				paramMap.put("couSelectMark", relationMap.get("couSelectMark"));
				courseDao.addMyStudyHistory(paramMap);
			}
			resultFlag = true;
		} catch (Exception e) {
			throw e;
		}
		return resultFlag;
	}
	
	/**
	 * @description 获取学生或教师学习了的章节的信息
	 * @param Map<String,Object> paramMap 工号或学号   课程号
	 * @return List
	 * @throws Exception
	 */
	public List<Map<String,Object>> getMyStudyChapterList(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = courseDao.getMyStudyChapterList(paramMap);
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description 获取某专业下的课程信息
	 * @param Map<String,Object> paramMap 专业id
	 * @return List
	 * @throws Exception
	 */
	public List<Map<String,Object>> getCourseByProfessionId(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = courseDao.getCourseByProfessionId(paramMap);
		}catch(Exception e){
			throw e; 
		}
		return list;
	}
	
	/**
	 * @description 修改课程的选课标识
	 * @param Map<String,Object> paramMap 课程id 1（不是选课的）或2（选课的课程）
	 * @return List
	 * @throws Exception
	 */
	public boolean saveSelectMark(Map<String, Object> paramMap) throws Exception{
		
		boolean result = false;
		try{
			courseDao.saveSelectMark(paramMap);
			result = true;
		}catch(Exception e){
			throw e; 
		}
		return result;
	}	
	
	/**
	 * @description 获取某个人的学习进度信息
	 * @param Map<String,Object> paramMap 课程id 学号
	 * @return List
	 * @throws Exception
	 */
	public Map<String, Object> getMyStudyProcess(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>(); 
		try{
			map = courseDao.getMyStudyProcess(paramMap);
		}catch(Exception e){
			throw e; 
		}
		return map;
	}	
	
	/**
	 * @description 查询我的消息列表
	 * @param Map<String,Object> paramMap 工号
	 * @return List
	 * @throws Exception
	 */
	public List<Map<String,Object>> getMyNotice(HttpServletRequest request) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"stuorteaId"}, request);
			list = courseDao.getMyNotice(sessionValues);
		}catch(Exception e){
			throw e;
		}
		return list;
	}
	

	/**
	 * @description 查询我的班级对应课程的列表（结课管理）
	 * @param Map<String,Object> paramMap 工号
	 * @return List
	 * @throws Exception
	 */
	public Map<String,Object> getMyClassCourseList(Map<String,Object> paramMap) throws Exception{
		
		Map<String, Object> map = new HashMap<String,Object>();
		try{
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取列表信息
			List<Map<String, Object>> classCourseList = courseDao.getMyClassCourseList(paramMap);
			map.put("classCourseList", classCourseList);
			map.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			paramMap.put("startNum", 0);
			List<Map<String, Object>> allList = courseDao.getMyClassCourseList(paramMap);
			int totalPage = pageUtil.getTotalPage(allList, pageSize);
			map.put("totalPage", totalPage);
		}catch(Exception e){
			throw e;
		}
		return map;
	}
	
	/**
	 * @description 将某班级对应的某课程进行完结
	 * @param Map<String,Object> paramMap 课程id 1（不是选课的）或2（选课的课程）
	 * @return List
	 * @throws Exception
	 */
	public boolean endCourseClass(Map<String, Object> paramMap, HttpServletRequest request) throws Exception{
		
		boolean result = false;
		try{
			//获取登录人信息
			SessionValues session = new SessionValues();
			Map<String, Object> sessionValues = session.getSessionValues(new String[]{"userName","stuorteaId"}, request);
			paramMap.put("classEndMark", 1);
			courseDao.endCourseClass(paramMap);
			//0626查询这个班级的全部信息
			Map<String, Object> classMap = classDao.getClassNameById(paramMap);
			//如果是选课的班级，需要将这个课程变为正常的课，并且删除这个课程的选课班级信息
			if(Integer.parseInt(classMap.get("classMark").toString()) == 1){
				paramMap.put("selectMark", 1);
				courseDao.saveSelectMark(paramMap);
				//修改新加的字段标识，将class表中这个班级的新加标识改为已结课
				paramMap.put("useMark", 1);
				classDao.saveUseMark(paramMap);
				//将这个班级下的学生与这个课程的关联的结课标识改为1
				classDao.saveEndMarkEnd(paramMap);
			}
//			//删除这个班级信息
//			List<String> idList = new ArrayList<String>();
//			idList.add(paramMap.get("classId").toString());
//			classDao.delClass(idList);
//			//删除这个课程与这个选课班级的关联信息
//			relationDao.delCourseReClass(paramMap);
			
			//如果这个课的上课老师有两种都上的，如果，classMark是1 就改为1 如果是0 改为2
			Map<String, Object> teacherMsg = relationDao.getTeachAllTeacherByCourseId(paramMap);
			if(teacherMsg != null){
				//如果是选课的班级，把老师与这个课的mark变为1
				if(Integer.parseInt(classMap.get("classMark").toString()) == 1){
					teacherMsg.put("selectMark", 1);
					relationDao.saveSelectMark(teacherMsg);
				//如果是正常的班级，把mark改为2	
				}else{
					teacherMsg.put("selectMark", 2);
					relationDao.saveSelectMark(teacherMsg);
				}
			}
			//删除classrelation中这个课，这个班，这个老师之间的关联
			sessionValues.put("classId", paramMap.get("classId"));
			sessionValues.put("courseId", paramMap.get("courseId"));
			classDao.delClassRelation(sessionValues);
			//0626end
			result = true;
		}catch(Exception e){
			throw e; 
		}
		return result;
	}	
	
	/**
	 * @description 通过课程，章节，查询课程课件信息
	 * @param Map<String,Object> paramMap 课程id 章节id
	 * @return List
	 * @throws Exception
	 */
	public Map<String, Object> getChapterByCourseId(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>(); 
		try{
			map = courseDao.getChapterByCourseId(paramMap);
		}catch(Exception e){
			throw e; 
		}
		return map;
	}	
	
	/**
	 * @description 查询所有课程
	 * @param Map<String,Object> paramMap 课程id 章节id
	 * @return List
	 * @throws Exception
	 */
	public List<Map<String, Object>> getAllCourse() throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 
		try{
			list = courseDao.getAllCourse();
		}catch(Exception e){
			throw e; 
		}
		return list;
	}	
	
	/**
	 * @description 是否删除作业，视频的方法
	 * @param Map<String,Object> delHomework, delVideo, courseId , chapterId
	 * @return List
	 * @throws Exception
	 */
	public boolean delHomeworkOrVideo(String delHomework, String delVideo, String delMaterial,String delAnswer,long courseId, long chapterId, long chapterNum) throws Exception{
		
		boolean result = false;
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("courseId", courseId);
			paramMap.put("chapterNum", chapterNum);
			Map<String, Object> chapterDetail = chapterDao.getChapterByClassNum(paramMap);
			//获取文件前缀
			String[] keysArray = {"server_address", "project_name","tomcatUrl"};
			Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			String url = (String)urlmap.get("tomcatUrl")+(String)urlmap.get("project_name")+"upload/course/";
			FileUpload fileUpload = new FileUpload();
			//如果删除了作业
			if(delHomework.equals("true")){
				paramMap.put("type", 3);
				//将字段清空
				chapterDao.delChapterCoHoVo(paramMap);
				System.out.println("url:"+url+"  homeworkName:"+(String)chapterDetail.get("homeworkName"));
				//将文件删除
				fileUpload.deleteFileOnly(url, (String)chapterDetail.get("homeworkName"));
				fileUpload.deleteFileOnly(url, (String)chapterDetail.get("homeworkNameDoc"));
			}
			//如果删除了视频
			if(delVideo.equals("true")){
				paramMap.put("type", 2);
				//将字段清空
				chapterDao.delChapterCoHoVo(paramMap);
				//将文件删除
				fileUpload.deleteFileOnly(url, (String)chapterDetail.get("videoName"));
			}
			//如果删除了备课材料
			if(delMaterial.equals("true")){
				paramMap.put("type", 5);
				//将字段清空
				chapterDao.delChapterCoHoVo(paramMap);
				//将文件删除
				fileUpload.deleteFileOnly(url, (String)chapterDetail.get("courseMaterial"));
			}
			//如果删除了课后答案
			if(delMaterial.equals("true")){
				paramMap.put("type", 6);
				//将字段清空
				chapterDao.delChapterCoHoVo(paramMap);
				//将文件删除
				fileUpload.deleteFileOnly(url, (String)chapterDetail.get("courseAnswer"));
			}
		}catch(Exception e){
			throw e; 
		}
		return result;
	}	
	
	/**
	 * @description 获取某个老师教的全部课程
	 * @param Map<String,Object> paramMap 工号
	 * @return List
	 * @throws Exception
	 */
	public List<Map<String,Object>> getMyClasses(Map<String, Object> paramMap) throws Exception{
		
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try{
			list = courseDao.getMyClasses(paramMap);
		}catch(Exception e){
			throw e;
		}
		return list;
	}
	@Transactional
	public String insertSkill(List<SkillDTO> list, Integer skillId, MultipartFile file, Integer role) {

		//level 1
		try {
			SkillNotes skillDTOnew0 = new SkillNotes();
			skillDTOnew0.setLevel(1);
			skillDTOnew0.setName(list.get(0).getPost());
			skillDTOnew0.setCreateTime(new Date());

			Integer id = courseDao.insertSkill(skillDTOnew0);
			System.out.println(id);
			//level 2
			SkillNotes skillDTOnew1 = new SkillNotes();
			skillDTOnew1.setLevel(2);
			skillDTOnew1.setParentId(skillDTOnew0.getId());
			skillDTOnew1.setName(list.get(0).getDescribe());
			skillDTOnew1.setCreateTime(new Date());
			courseDao.insertSkill(skillDTOnew1);
			System.out.println(2);
			//level 3
			int count = 0;
			Map<String, Integer> map = new HashMap<String, Integer>();
			for (SkillDTO skillDTO : list) {
				count++;
				if (!"".equals(skillDTO.getSkillModule())) {
					SkillNotes skillDTOnew2 = new SkillNotes();
					skillDTOnew2.setLevel(3);
					skillDTOnew2.setParentId(skillDTOnew1.getId());
					skillDTOnew2.setName(skillDTO.getSkillModule());
					skillDTOnew2.setCreateTime(new Date());
					courseDao.insertSkill(skillDTOnew2);
					map.put(StringUtils.join(count), skillDTOnew2.getId());
				}
			}
			Map map4 = querySkillStutas(map, list.size() + 1);
			System.out.println(map4.toString());
			//level 4
			Map<String, Integer> map1 = new HashMap<String, Integer>();
			int count1 = 0;
			for (SkillDTO skillDTO : list) {
				count1++;
				if (!"".equals(skillDTO.getSubSkill())) {
					SkillNotes skillDTOnew3 = new SkillNotes();
					skillDTOnew3.setLevel(4);
					skillDTOnew3.setParentId(queryParentId(map4, count1));
					skillDTOnew3.setName(skillDTO.getSubSkill());
					courseDao.insertSkill(skillDTOnew3);
					map1.put(StringUtils.join(count1), skillDTOnew3.getId());
				}
			}
			Map map5 = querySkillStutas(map1, list.size() + 1);
			System.out.println(map5.toString());
			//level 5
			int count2 = 0;
			Map<String, Integer> map2 = new HashMap<String, Integer>();
			for (SkillDTO skillDTO : list) {
				count2++;
				if (!"".equals(skillDTO.getKnowledgePoints())) {
					SkillNotes skillDTOnew4 = new SkillNotes();
					skillDTOnew4.setLevel(5);
					skillDTOnew4.setParentId(queryParentId(map5, count2));
					skillDTOnew4.setName(skillDTO.getKnowledgePoints());
					courseDao.insertSkill(skillDTOnew4);
					map2.put(StringUtils.join(count2), skillDTOnew4.getId());
				}
			}
			Map map6 = querySkillStutas(map2, list.size() + 1);
			System.out.println(map6.toString());
			//level 6
			int count3 = 0;
			for (SkillDTO skillDTO : list) {
				count3++;
				if (!"".equals(skillDTO.getKnowledgePoints())) {
					SkillNotes skillDTOnew5 = new SkillNotes();
					skillDTOnew5.setLevel(6);
					skillDTOnew5.setParentId(queryParentId(map6, count3));
//					List<Map<String, Object>> listmap = courseDao.querySkillCourse(null, skillDTO.getCourseName());
					String idnew = "0";
					String key_value = "";
					skillDTOnew5.setIconValue(key_value);
					skillDTOnew5.setCourseId(Integer.valueOf(idnew));
					skillDTOnew5.setName(skillDTO.getCourseName());
					courseDao.insertSkill(skillDTOnew5);

				}
			}

			return "";
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "fail";
		}

	}

	public Map querySkillStutas(Map<String, Integer> map, Integer count) {
		Map<String, Integer> mapnew = map.entrySet()
				.stream()
				.sorted(Map.Entry.comparingByValue())
				.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
		List list = Lists.newArrayList();
		for (String key : mapnew.keySet()) {
			list.add(key);
		}
		list.add(count);
		String countold = "";
		Map map1 = new HashMap();
		if (org.apache.commons.collections.CollectionUtils.isNotEmpty(list)) {
			for (int i = 0; i < 1; i++) {
				for (int j = 1; j < list.size(); j++) {
					if (j == 1) {
						map1.put(list.get(i).toString() + "-" + list.get(j).toString(), mapnew.get(list.get(i).toString()));
					} else {
						if (Integer.valueOf(list.get(j).toString()) == count) {
							map1.put(countold + "-" + list.get(j).toString(), mapnew.get(countold));
						} else {
							map1.put(countold + "-" + list.get(j).toString(), mapnew.get(countold));
						}
					}
					countold = list.get(j).toString();
				}
			}
		}
		return map1;
	}

	private Integer queryParentId(Map<String, Integer> map, Integer count) {
		Integer value = null;
		for (String countnew : map.keySet()) {
			String[] split = countnew.split("-");
			if (Integer.valueOf(split[1]) == 1 && count == 1) {
				value = Integer.valueOf(map.get(countnew).toString());
			} else {
				for (int i = Integer.valueOf(split[0]); i < Integer.valueOf(split[1]); i++) {
					if (count == i) {
						value = Integer.valueOf(map.get(countnew).toString());
					}
				}
			}
		}
		return value;
	}

	public List<SkillNotes> querySkill(Integer id) {
		List<SkillNotes> childLists = null;
		childLists = courseDao.getChildLists(id);
		if (id == null) {
			childLists = courseDao.getChildListsnew();
		}
		return childLists;
	}

	public void updateSkill(Integer id, String name) {
		courseDao.updateSkill(name, id, null);
	}
	public Map<String, List<SkillNotes>> querySkillOther(Integer id) {

		List<SkillNotes> childLists = courseDao.getChildLists(id);
		if (childLists != null && childLists.size() > 0 && childLists.get(0).getChildrenList() != null && childLists.get(0).getChildrenList().size() > 0) {
			List<SkillNotes> childrenList = childLists.get(0).getChildrenList();
			List<SkillNotes> childrenList1 = childrenList.get(0).getChildrenList();
			Map map = new HashMap();
			for (SkillNotes skillNotes : childrenList1) {
				List<SkillNotes> list = Lists.newArrayList();
				if (skillNotes.getChildrenList().size() > 0) {
					List<SkillNotes> skillNotes3 = querySkillInfoOther(skillNotes.getChildrenList());
					for (SkillNotes skillNotes1 : skillNotes3) {
						if (skillNotes1 != null && skillNotes1.getChildrenList().size() > 0 && skillNotes1.getChildrenList().get(0).getChildrenList().size() > 0) {
							SkillNotes skillNotes2 = new SkillNotes();
							String courseName = skillNotes1.getChildrenList().get(0).getChildrenList().size() == 0 ? "" : skillNotes1.getChildrenList().get(0).getChildrenList().get(0).getName();
							skillNotes2.setName(skillNotes1.getChildrenList().get(0).getChildrenList().size() == 0 ? "" : skillNotes1.getChildrenList().get(0).getChildrenList().get(0).getName());
							skillNotes2.setIconValue("");
							list.add(skillNotes2);
						}
					}
				}
				map.put(skillNotes.getId(), list);

			}
			return map;
		}
		return null;
	}
	private List<SkillNotes> querySkillInfoOther(List<SkillNotes> skillNoteslist) {
		// 从list中索引为0开始往后遍历
		for (int i = 0; i < skillNoteslist.size() - 1; i++) {
			// 从list中索引为 list.size()-1 开始往前遍历
			for (int j = skillNoteslist.size() - 1; j > i; j--) {
				// 进行比较
				if (skillNoteslist.get(j).getChildrenList().size() > 0 && skillNoteslist.get(j).getChildrenList().get(0).getChildrenList().size() > 0 && skillNoteslist.get(i).getChildrenList().size() > 0 && skillNoteslist.get(i).getChildrenList().get(0).getChildrenList().size() > 0) {
					if (skillNoteslist.get(j).getChildrenList().get(0).getChildrenList().get(0).getName().equals(skillNoteslist.get(i).getChildrenList().get(0).getChildrenList().get(0).getName())) {
						// 去重
						skillNoteslist.remove(j);
					}
				}
			}
		}
		return skillNoteslist;
	}
}